package com.yy.jhl.ui.manager.activity;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.SPUtils;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.common.commonwidget.ViewPagerFixed;
import com.yy.jhl.R;
import com.yy.jhl.app.AppConstant;
import com.yy.jhl.bean.CGSOrderInfo;
import com.yy.jhl.bean.JGOrderInfo;
import com.yy.jhl.interfaces.DoneOrderCallBack;
import com.yy.jhl.interfaces.MessageNumCallBack;
import com.yy.jhl.ui.manager.adapter.JGOrderAdapter;
import com.yy.jhl.ui.manager.adapter.RWOrderAdapter;
import com.yy.jhl.ui.manager.adapter.ViewPageAdapter;
import com.yy.jhl.ui.manager.contract.JGOrderListContract;
import com.yy.jhl.ui.manager.contract.MenuTwoJGContract;
import com.yy.jhl.ui.manager.presenter.JGOrderListPresenter;
import com.yy.jhl.ui.manager.presenter.MenuTwoJGPresenter;
import com.yy.jhl.ui.qrcode.QRScanActivity;
import com.yy.jhl.util.DataRequestUtil;
import com.yy.jhl.widget.FullListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**任务订单
 * Created by libingn on 2017/12/4.
 */

public class MenuTwoRWActivity extends BaseActivity<JGOrderListPresenter> implements JGOrderListContract.View, AdapterView.OnItemClickListener,PullToRefreshBase.OnRefreshListener2, ViewPager.OnPageChangeListener{
    @BindView(R.id.doing_type_text)
    TextView doingTypeText;
    @BindView(R.id.done_type_text)
    TextView doneTypeText;
    @BindView(R.id.viewPage)
    ViewPagerFixed viewPage;
    FullListView doingFullListView,doneFullListView;
    RWOrderAdapter doingOrderAdapter,doneOrderAdapter;
    PullToRefreshListView doingRefreshListView,doneRefreshListView;
    ListView doingListView,doneListView;
    private int status = 2;//2.进行中 3.已完结
    private int doingPage = 1;
    private int donePage = 1;
    private boolean isFistLoad = true;
    @Override
    public int getLayoutId() {
        return R.layout.act_menu_two;
    }

    @Override
    public void initView() {
        initViewPage();
        showTitle("任务订单");
        showLeftImageView(R.mipmap.message_icon, new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                startActivity(MessageListActivity.class);
            }
        });
        showRightImageView(R.mipmap.scan_white_icon, new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                startActivityForResult(QRScanActivity.class, 1000);
            }
        });
        mPresenter.getOrderList(2,status,1,"",true);
    }
    @Override
    protected void onResume() {
        super.onResume();
        DataRequestUtil.getInstance(mContext).getUnReadMessageNum(new MessageNumCallBack() {
            @Override
            public void getCallBack(int total) {
                if (total == 0){
                    showUnreadImageView(R.drawable.red_round_bg,false);
                }else {
                    showUnreadImageView(R.drawable.red_round_bg,true);
                }
            }
        });
    }
    /**
     * 初始化ViewPage控件
     */
    private void initViewPage(){
        List<View> viewList = new ArrayList<>();
        doingFullListView = new FullListView(mContext);
        doneFullListView = new FullListView(mContext);
        doingRefreshListView = doingFullListView.getPullListView();
        doneRefreshListView = doneFullListView.getPullListView();
        doingRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        doneRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        doingListView = doingRefreshListView.getRefreshableView();
        doneListView = doneRefreshListView.getRefreshableView();
        doingOrderAdapter = new RWOrderAdapter(mContext, false);
        doingListView.setAdapter(doingOrderAdapter);
        doneOrderAdapter = new RWOrderAdapter(mContext, true);
        doneListView.setAdapter(doneOrderAdapter);
        viewList.add(doingFullListView);
        viewList.add(doneFullListView);
        viewPage.setAdapter(new ViewPageAdapter(viewList));
        viewPage.setOnPageChangeListener(this);
        doingRefreshListView.setOnRefreshListener(this);
        doneRefreshListView.setOnRefreshListener(this);
        doingRefreshListView.setOnItemClickListener(this);
        doneRefreshListView.setOnItemClickListener(this);
        //默认显示排行榜
        viewPage.setCurrentItem(0);
        showSelectView(true,false);
    }
    /**
     * 根据滑动显示不同标题
     */
    private void showSelectView(boolean isDoing,boolean isDone){
        doingTypeText.setTextColor(isDoing ? getResources().getColor(R.color.white) : getResources().getColor(R.color.green_color));
        doingTypeText.setBackgroundResource(isDoing ? R.drawable.green_left_bg : R.drawable.white_left_bg);
        doneTypeText.setTextColor(isDone ? getResources().getColor(R.color.white) : getResources().getColor(R.color.green_color));
        doneTypeText.setBackgroundResource(isDone ? R.drawable.green_right_bg : R.drawable.white_right_bg);
        if (isDone){
            if (isFistLoad){
                isFistLoad = false;
                status = 3;
                mPresenter.getOrderList(2,status,donePage,"",false);
            }
        }
    }
    @OnClick({R.id.doing_type_text, R.id.done_type_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.doing_type_text:
                status = 2;
                viewPage.setCurrentItem(0);
                showSelectView(true,false);
                break;
            case R.id.done_type_text:
                status = 3;
                viewPage.setCurrentItem(1);
                showSelectView(false,true);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            status = 2;
            showSelectView(true,false);
        } else if (position == 1){
            status = 3;
            showSelectView(false,true);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        //刷新
        switch (viewPage.getCurrentItem()) {
            case 0:
                status = 2;
                doingOrderAdapter.clearData();
                doingPage = 1;
                mPresenter.getOrderList(2,status,doingPage,"",false);
                break;
            case 1:
                status = 3;
                doneOrderAdapter.clearData();
                donePage = 1;
                mPresenter.getOrderList(2,status,donePage,"",false);
                break;
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        //加载更多
        switch (viewPage.getCurrentItem()) {
            case 0:
                status = 2;
                doingPage ++;
                mPresenter.getOrderList(2,status,doingPage,"",false);
                break;
            case 1:
                status = 3;
                donePage ++;
                mPresenter.getOrderList(2,status,donePage,"",false);
                break;
        }
    }
    @Override
    public void getOrderListSuccess(List<JGOrderInfo> orderInfos) {
        switch (status) {
            case 2:
                if (doingPage == 1){
                    doingOrderAdapter.clearData();
                    if (orderInfos == null || orderInfos.size() == 0){
                        doingFullListView.showNoDataView();
                    }else {
                        doingFullListView.showDataView();
                    }
                }
                doingOrderAdapter.addData(orderInfos);
                doingRefreshListView.onRefreshComplete();
                break;
            case 3:
                if (donePage == 1){
                    doneOrderAdapter.clearData();
                    if (orderInfos == null || orderInfos.size() == 0){
                        doneFullListView.showNoDataView();
                    }else {
                        doneFullListView.showDataView();
                    }
                }
                doneOrderAdapter.addData(orderInfos);
                doneRefreshListView.onRefreshComplete();
                break;
        }
//        if (isFistLoad){
//            isFistLoad = false;
//            status = 3;
//            mPresenter.getOrderList(2,status,donePage,"",false);
//        }
    }

    @Override
    public void getOrderListFail() {
//        if (isFistLoad){
//            isFistLoad = false;
//            mPresenter.getOrderList(2,status,doingPage,"",false);
//        }
        switch (viewPage.getCurrentItem()) {
            case 0:
                doingRefreshListView.onRefreshComplete();
                doingFullListView.showNoDataView();
                break;
            case 1:
                doneRefreshListView.onRefreshComplete();
                doneFullListView.showNoDataView();
                break;
        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            String result = data.getStringExtra(QRScanActivity.EXTRA_QRCODE_SUCCESS);
            switch (requestCode) {
                case 1000:
                    if (result.equals("***")){
                        showShortToast("扫描结果格式有误！");
                    }else {
                        InviteBoxInfoActivity.startAction(mContext,result);
                    }
                    break;
            }
        }
    }
}
